home *** CD-ROM | disk | FTP | other *** search
- #include <sys/types.h>
-
- #include <signal.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <termio.h>
- #include <unistd.h>
- #include <sys/bsdtypes.h>
- #ifdef SPASSWD
- #include <shadow.h>
- #endif
- #include <sys/fs/s5dir.h>
- #include <sys/user.h>
-
- #include "sys5unix.h"
- #include "global.h"
- #include "iface.h"
- #include "timer.h"
- #include "files.h"
- #include "hardware.h"
- #include "main.h"
- #include "hpux.h"
-
- extern int Debug;
- extern long sigsetmask();
- extern void _exit();
- void abort();
-
- #define TIMEOUT 120
-
- static int chkread[2];
- static int actread[2];
- static void (*readfnc[_NFILE]) __ARGS((void *));
- static void *readarg[_NFILE];
-
- static int chkwrite[2];
- static int actwrite[2];
- static void (*writefnc[_NFILE]) __ARGS((void *));
- static void *writearg[_NFILE];
-
- static int chkexcp[2];
- static int actexcp[2];
- static void (*excpfnc[_NFILE]) __ARGS((void *));
- static void *excparg[_NFILE];
-
- static int nfds = -1;
-
- static int local_kbd;
-
- static struct termio curr_termio;
- static struct termio prev_termio;
-
- static void check_files_changed __ARGS((void));
-
- /*---------------------------------------------------------------------------*/
- void ioinit()
- {
-
- int i;
-
- if (local_kbd = isatty(0)) {
- ioctl(0, TCGETA, &prev_termio);
- curr_termio = prev_termio;
- curr_termio.c_iflag = BRKINT | ICRNL | IXON | IXANY | IXOFF;
- curr_termio.c_lflag = 0;
- curr_termio.c_cc[VMIN] = 0;
- curr_termio.c_cc[VTIME] = 0;
- ioctl(0, TCSETA, &curr_termio);
- fflush(stdout);
- on_read(0, keyboard, (void *) 0);
- } else {
- for (i = 0; i < _NFILE; i++) close(i);
- setpgrp();
- fopen("/dev/null", "r+");
- fopen("/dev/null", "r+");
- fopen("/dev/null", "r+");
- remote_kbd_initialize();
- }
- setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
- signal(SIGPIPE, SIG_IGN);
- signal(SIGALRM, abort);
- signal (SIGCLD, sigchild_handler);
- if (!Debug) alarm(TIMEOUT);
- umask(022);
- if (!getenv("HOME"))
- putenv("HOME=/users/root");
- if (!getenv("LOGNAME"))
- putenv("LOGNAME=root");
- if (!getenv("PATH"))
- putenv("PATH=/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin");
- if (!getenv("SHELL"))
- putenv("SHELL=/bin/sh");
- if (!getenv("TZ"))
- putenv("TZ=MEZ-1MESZ");
- timerproc(); /* Init times */
- fixutmpfile();
- }
-
- /*---------------------------------------------------------------------------*/
-
- void iostop()
- {
- register struct iface *ifp;
-
- if (local_kbd) {
- ioctl(0, TCSETA, &prev_termio);
- fflush(stdout);
- }
- for (ifp = Ifaces; ifp; ifp = ifp->next)
- if (ifp->stop) (*ifp->stop)(ifp);
- }
-
- /*---------------------------------------------------------------------------*/
- int system(cmdline)
- char *cmdline;
- {
-
- int i, pid, status;
- long oldmask;
-
- if (!cmdline) return 1;
- sighold(SIGINT);
- switch (pid = fork()) {
- case -1:
- sigrelse(SIGINT);
- return (-1);
- case 0:
- for (i = 3; i < _NFILE; i++) close(i);
- sighold(SIGINT);
- execl("/bin/sh", "sh", "-c", cmdline, (char *) 0);
- sigrelse(SIGINT);
- _exit(127);
- default:
- sighold(SIGINT);
- while (wait3(&status, -1, NULL) != pid)
- sleep(-15); /* Wait some ticks */
- sigrelse(SIGINT);
- return status;
- }
- }
-
- /*---------------------------------------------------------------------------*/
-
- int _system(cmdline)
- char *cmdline;
- {
- return system(cmdline);
- }
-
- /*---------------------------------------------------------------------------*/
-
- int doshell(argc, argv, p)
- int argc;
- char *argv[];
- void *p;
- {
- char buf[2048];
-
- *buf = '\0';
- while (--argc > 0) {
- if (*buf) strcat(buf, " ");
- strcat(buf, *++argv);
- }
- return system(buf);
- }
-
- /*---------------------------------------------------------------------------*/
-
- #define setmask(mask, fd) ((mask)[(fd)>>5] |= (1 << ((fd) & 31)))
- #define clrmask(mask, fd) ((mask)[(fd)>>5] &= ~(1 << ((fd) & 31)))
- #define maskset(mask, fd) ((mask)[(fd)>>5] & (1 << ((fd) & 31)))
-
- /*---------------------------------------------------------------------------*/
-
- void on_read(fd, fnc, arg)
- int fd;
- void (*fnc) __ARGS((void *));
- void *arg;
- {
- readfnc[fd] = fnc;
- readarg[fd] = arg;
- setmask(chkread, fd);
- clrmask(actread, fd);
- nfds = -1;
- }
-
- /*---------------------------------------------------------------------------*/
-
- void off_read(fd)
- int fd;
- {
- readfnc[fd] = 0;
- readarg[fd] = 0;
- clrmask(chkread, fd);
- clrmask(actread, fd);
- nfds = -1;
- }
-
- /*---------------------------------------------------------------------------*/
-
- void on_write(fd, fnc, arg)
- int fd;
- void (*fnc) __ARGS((void *));
- void *arg;
- {
- writefnc[fd] = fnc;
- writearg[fd] = arg;
- setmask(chkwrite, fd);
- clrmask(actwrite, fd);
- nfds = -1;
- }
-
- /*---------------------------------------------------------------------------*/
-
- void off_write(fd)
- int fd;
- {
- writefnc[fd] = 0;
- writearg[fd] = 0;
- clrmask(chkwrite, fd);
- clrmask(actwrite, fd);
- nfds = -1;
- }
-
- /*---------------------------------------------------------------------------*/
-
- void on_excp(fd, fnc, arg)
- int fd;
- void (*fnc) __ARGS((void *));
- void *arg;
- {
- excpfnc[fd] = fnc;
- excparg[fd] = arg;
- setmask(chkexcp, fd);
- clrmask(actexcp, fd);
- nfds = -1;
- }
-
- /*---------------------------------------------------------------------------*/
-
- void off_excp(fd)
- int fd;
- {
- excpfnc[fd] = 0;
- excparg[fd] = 0;
- clrmask(chkexcp, fd);
- clrmask(actexcp, fd);
- nfds = -1;
- }
-
- /*---------------------------------------------------------------------------*/
-
- static void check_files_changed()
- {
-
- int changed = 0;
- static long nexttime, net_time, rc_time;
- struct stat statbuf;
-
- if (Debug || nexttime > secclock()) return;
- nexttime = secclock() + 600;
-
- if (stat("/tcp/net", &statbuf)) return;
- if (!net_time) net_time = statbuf.st_mtime;
- if (net_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
- changed = 1;
-
- if (stat(Startup, &statbuf)) return;
- if (!rc_time) rc_time = statbuf.st_mtime;
- if (rc_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
- changed = 1;
-
- if (changed) doexit(0, (char **) 0, (void *) 0);
- }
-
- /*---------------------------------------------------------------------------*/
-
- void eihalt()
- {
-
- int n;
- int status;
- int32 nte;
- register int nfds;
- register unsigned int i;
- struct timeval timeout;
-
- check_files_changed();
- wait3(&status, WNOHANG, (int *) 0);
- if (!Debug) alarm(TIMEOUT);
- if (chkread[1] | chkwrite[1] | chkexcp[1]) {
- i = chkread[1] | chkwrite[1] | chkexcp[1];
- nfds = 32;
- } else {
- i = chkread[0] | chkwrite[0] | chkexcp[0];
- nfds = 0;
- }
- for (n = 16; n; n >>= 1)
- if (i & ((-1) << n)) {
- nfds += n;
- i >>= n;
- }
- if (i) nfds++;
- actread [0] = chkread [0];
- actread [1] = chkread [1];
- actwrite[0] = chkwrite[0];
- actwrite[1] = chkwrite[1];
- actexcp [0] = chkexcp [0];
- actexcp [1] = chkexcp [1];
- timeout.tv_sec = 0;
- if (Hopper)
- timeout.tv_usec = 0;
- else {
- nte = next_timer_event();
- if (nte > 999) nte = 999;
- timeout.tv_usec = 1000 * nte;
- }
- if (select(nfds, actread, actwrite, actexcp, &timeout) < 1) {
- actread [0] = actread [1] = 0;
- actwrite[0] = actwrite[1] = 0;
- actexcp [0] = actexcp [1] = 0;
- } else
- for (i = 0; i < nfds; i++) {
- if (readfnc [i] && maskset(actread , i)) (*readfnc [i])(readarg [i]);
- if (writefnc[i] && maskset(actwrite, i)) (*writefnc[i])(writearg[i]);
- if (excpfnc [i] && maskset(actexcp , i)) (*excpfnc [i])(excparg [i]);
- }
- }
-
-
- /* some other things */
-
- rename(s1, s2)
- char *s1, *s2;
- {
- char tmp[50];
- int i;
- unlink(s2);
- i = link(s1, s2);
- if(i == 0){
- unlink(s1);
- }
- return(i);
- }
-
- /* wait stuff */
-
- #define MAXZOMBIE 32
- struct zombie {
- int pid;
- int status;
- } zombie [MAXZOMBIE];
-
- int wait3 (statloc, options, dummy)
- int *statloc;
- int options;
- int *dummy; {
- int i, pid;
-
- do {
- for (i = 0; i < MAXZOMBIE; i++)
- if (zombie[i].pid) {
- if (statloc)
- *statloc = zombie[i].status;
- pid = zombie[i].pid;
- zombie[i].pid = 0;
- return pid;
- }
- } while (!options);
- return 0;
- }
-
- int
- sigchild_handler(sig, code)
- int sig;
- int code; {
- int victim, status, i;
- victim = wait (&status);
- signal (sig, sigchild_handler);
- for (i = 0; i < MAXZOMBIE; i++)
- if (!zombie[i].pid) {
- zombie[i].status = status;
- zombie[i].pid = victim;
- return;
- }
- }
- #ifdef SPASSWD
- /** /etc/shadow processing **/
-
- struct spwd *getspwdentry(name)
- char *name;
- {
-
- #define DEFAULTUSER "guest"
-
- FILE * fp;
- char *cp;
- char username[128];
- int fd;
- int uid;
- struct spwd *sw;
-
- /* Fix user name */
-
- for (cp = username; isalnum(uchar(*name)); *cp++ = tolower(uchar(*name++))) ;
- *cp = '\0';
- if (!isalpha(uchar(*username)) || strlen(username) > 8)
- strcpy(username, DEFAULTUSER);
-
- /* Search existing shadow entry */
-
- while ((sw = getspent()) && strcmp(username, sw->sp_namp)) ;
- endspent();
- if (sw) return sw;
- return 0;
- }
- #endif
-
- #ifndef RESTRICTED /* ISC with bug allowing to set the u_uid etc. to 0 */
- int
- setresuid(r,e,s)
- int r,e,s;
- {
- struct user *user;
-
- user = (struct user *) 0xE0000000;
-
- user->u_uid = r;
- user->u_ruid = e;
- return 0;
- }
- int
- setresgid(r,e,s)
- int r,e,s;
- {
-
- struct user *user;
-
- user = (struct user *) 0xE0000000;
-
- user->u_gid = r;
- user->u_rgid = e;
- return 0;
- }
- #else
-
- int setresuid(r,e,s)
- int r,e,s;
- {
- }
-
- int setresgid(r,e,s)
- int r,e,s;
- {
- }
-
- #endif /* RESTRICTED */
- /* Some dummy functions */
-
- unsigned long strtoul (str, ptr, base)
- char *str, **ptr;
- int base;
- {
- return strtol(str, ptr, base);
- }
- rtprio()
- {
- }
-
- settimeofday()
- {
- }
-